package com.moji.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.moji.common.entity.MyPage;
import com.moji.common.utils.PageUtil;
import com.moji.entity.MjFlow;
import com.moji.entity.MjFlowConfig;
import com.moji.entity.dto.QueryFlowConfigDto;
import com.moji.mapper.MjFlowConfigMapper;
import com.moji.service.MjFlowConfigService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.moji.util.IdGenerator;
import com.moji.util.MoJiCommonUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 流程审批表 服务实现类
 * </p>
 *
 * @author ccl
 * @since 2024-07-27
 */
@Service
public class MjFlowConfigServiceImpl extends ServiceImpl<MjFlowConfigMapper, MjFlowConfig> implements MjFlowConfigService {

    @Resource
    private IdGenerator idGenerator;

    @Override
    public MyPage<MjFlowConfig> queryByPage(QueryFlowConfigDto dto) {
        LambdaQueryWrapper<MjFlowConfig> wrapper = new LambdaQueryWrapper<>();
        //流程名称
        wrapper.like(ObjectUtil.isNotEmpty(dto.getFlowName()),MjFlowConfig::getFlowName
                ,dto.getFlowName());
        //创建时间倒叙
        wrapper.orderByDesc(MjFlowConfig::getCreateTime);
        PageHelper.startPage(dto.getPageNum(), dto.getPageSize());
        List<MjFlowConfig> mjFlows = list(wrapper);
        PageInfo<MjFlowConfig> info = new PageInfo<>(mjFlows);
        return PageUtil.PageInfo2MyPage(info);
    }

    @Override
    public boolean delete(Long id) {
        if(ObjectUtil.isEmpty(id)){
            throw new RuntimeException("主键ID不能为空,请核查!");
        }
        return this.removeById(id);
    }

    @Override
    public boolean add(MjFlowConfig mjFlowConfig) {
        String flowCode= idGenerator.idGenerator("SPPZ");
        mjFlowConfig.setFlowCode(flowCode);
        mjFlowConfig.setCreateId(MoJiCommonUtils.getUserCode());
        mjFlowConfig.setUpdateId(MoJiCommonUtils.getUserCode());
        mjFlowConfig.setCreateTime(new Date());
        mjFlowConfig.setUpdateTime(new Date());
        return this.save(mjFlowConfig);
    }

    @Override
    public boolean edit(MjFlowConfig mjFlowConfig) {
        MjFlowConfig mjFlowConfig1=this.getById(mjFlowConfig.getId());
        BeanUtils.copyProperties(mjFlowConfig,mjFlowConfig1);
        mjFlowConfig.setUpdateId(MoJiCommonUtils.getUserCode());
        mjFlowConfig.setUpdateTime(new Date());
        return this.updateById(mjFlowConfig);
    }

    @Override
    public MjFlowConfig detail(Long id) {
        if(ObjectUtil.isEmpty(id)){
            throw new RuntimeException("主键ID不能为空,请核查!");
        }
        return this.getById(id);
    }
}
